package com.zhibi.util.codec.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil {
	
	/**
	 * SHA256  摘要
	 * @param content
	 * @return
	 */
	public static String SHA256(String content) {
            MessageDigest digest = null;
			try {
				digest = java.security.MessageDigest
				        .getInstance("SHA-256");
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
            digest.update(content.getBytes());
            byte messageDigest[] = digest.digest();
            return HexStrUtil.parseByte2HexStr(messageDigest);
    }
	
	/**
	 * SHA1 摘要
	 * @param content
	 * @return
	 */
	public static String SHA1(String content) {
        try {
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("SHA-1");
            digest.update(content.getBytes());
            byte messageDigest[] = digest.digest();
            return HexStrUtil.parseByte2HexStr(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
	
	/**
	 * sha 摘要
	 * @param content
	 * @return
	 */
	public static String SHA(String content) {
        try {
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("SHA");
            digest.update(content.getBytes());
            byte messageDigest[] = digest.digest();
            return HexStrUtil.parseByte2HexStr(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

	/**
	 * MD5
	 * @param content
	 * @return
	 */
	public static String MD5(String content) {
        try {
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(content.getBytes());
            // 获得密文
            byte[] md = mdInst.digest();
            return HexStrUtil.parseByte2HexStr(md);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
